package com.prs.crm.service.sale.impl;

import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import com.prs.crm.context.Constrains;
import com.prs.crm.dao.sale.SalesStoreOutDao;
import com.prs.crm.domain.flow.WorkFlowProcess;
import com.prs.crm.domain.sale.SalesShipping;
import com.prs.crm.domain.sale.SalesShippingDetail;
import com.prs.crm.domain.sale.SalesStoreOut;
import com.prs.crm.domain.sale.SalesStoreOutDetail;
import com.prs.crm.domain.stock.Product;
import com.prs.crm.domain.stock.ProductStore;
import com.prs.crm.domain.store.StoreRoom;
import com.prs.crm.service.impl.FlowServiceImpl;
import com.prs.crm.service.sale.SalesShippingService;
import com.prs.crm.service.sale.SalesStoreOutService;
import com.prs.crm.service.stock.ProductStoreService;

public class SalesStoreOutServiceImpl extends
		FlowServiceImpl<SalesStoreOut, SalesStoreOutDao> implements
		SalesStoreOutService {

	@Autowired
	private ProductStoreService productStoreService;
	private SalesShippingService salesShippingService;

	@Override
	public void onProcessEnd(WorkFlowProcess process) {

		super.onProcessEnd(process);
		SalesStoreOut salesStoreOut = this.getProcessEntity(process);
		Set<SalesStoreOutDetail> details = salesStoreOut.getDetails();
		SalesShipping salesShipping = salesStoreOut.getSalesShipping();
		Set<SalesShippingDetail> details1 = salesShipping.getDetails();
		if (process.getResult() == Constrains.WORKFLOWPROCESS_RESULT_PASS) {
			//for (SalesStoreOutDetail detail : details) {
				//ProductStore psObject = this.getProductStoreService().getStore(
					//	salesStoreOut.getStoreRoom(),
					//	detail.getProduct(),
					//	salesStoreOut.getSalesShipping().getSalesContract()
								//.getBatchNum());

			//	psObject.setProductNum(psObject.getProductNum()
					//	- detail.getShippingNum());
			//	this.getProductStoreService().save(psObject);
			//}
		} else {
			for (SalesStoreOutDetail detail : details) {
				for (SalesShippingDetail detail2 : details1) {
					if (detail.getProduct() == detail2.getProduct()) {
						detail2.setOutNum(detail2.getOutNum()
								- detail.getShippingNum());
						detail2.setOutState(0);
					}
				}
			}
			salesShipping.setOutState(0);
			this.getSalesShippingService().save(salesShipping);
		}
	}

	@Override
	public void saveAllSaleStoreOut(SalesStoreOut salesStoreOut,
			Product[] products, String[] productNames, Integer[] storeNums,
			Integer[] ContractNums, StoreRoom[] rooms) {
		SalesShipping salesShipping = salesStoreOut.getSalesShipping();
		Set<SalesShippingDetail> details = salesShipping.getDetails();
		boolean b = true;
		for (int i = 0; i < products.length; i++) {
			SalesStoreOutDetail detalsObject = new SalesStoreOutDetail();
			detalsObject.setProduct(products[i]);
			detalsObject.setProductCode(productNames[i]);
			detalsObject.setProductName(productNames[i]);
			detalsObject.setStoreNum(storeNums[i]);
			detalsObject.setShippingNum(ContractNums[i]);
			salesStoreOut.addDetail(detalsObject);
			for (SalesShippingDetail detail : details) {
				if (detail.getProduct() == products[i]) {
					if (detail.getNumber() == ContractNums[i]) {
						detail.setOutState(1);
					}
					detail.setOutNum(detail.getOutNum() + ContractNums[i]);
				}
			}

		}
		for (SalesShippingDetail detail : details) {
			if (detail.getNumber() - detail.getOutNum() != 0) {
				b = false;
				break;
			}
		}
		if (b == true) {
			salesShipping.setOutState(1);
		}
		this.getSalesShippingService().save(salesShipping);
		super.save(salesStoreOut);
	}

	public boolean valideSubmit(SalesStoreOut entity) {

		return true;
	}

	public ProductStoreService getProductStoreService() {
		return productStoreService;
	}

	public void setProductStoreService(ProductStoreService productStoreService) {
		this.productStoreService = productStoreService;
	}

	public SalesShippingService getSalesShippingService() {
		return salesShippingService;
	}

	public void setSalesShippingService(
			SalesShippingService salesShippingService) {
		this.salesShippingService = salesShippingService;
	}

}
